﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Coupon
{
    public partial class CouponTicketDetail : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected ShopCouponTicket Ticket { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            int uid = 0;
            int ruleid = 0;

            _framePage = new FrameHelper(this);

            if (!IsPostBack)
            {
                //设置对象
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (int.TryParse(Request["uid"], out uid))
                    {
                        Ticket = session.Get<ShopCouponTicket>(uid);

                        //保存uid
                        ViewState["uid"] = uid;
                    }
                    else
                    {
                        if (!int.TryParse(Request["ruleid"], out ruleid))
                        {
                            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-参数错误" });
                            return;
                        }

                        var rule = session.Get<ShopCouponRule>(ruleid);

                        if (rule == null)
                        {
                            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-参数错误" });
                            return;
                        }

                        ViewState["ruleid"] = ruleid;
                    }
                }
            }
            else
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (ViewState["uid"] != null)
                    {
                        Ticket = session.Get<ShopCouponTicket>((int)ViewState["uid"]);

                        if (Ticket == null)
                        {
                            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-参数错误" });
                            return;
                        }
                    }
                    else
                    {
                        Ticket = new ShopCouponTicket();
                        Ticket.CreateDate = DateTime.Now;
                        Ticket.OrderMinAmount = 0;

                        ruleid = (int)ViewState["ruleid"];

                        var ruleDb = session.Get<ShopCouponRule>(ruleid);

                        if (ruleDb == null)
                        {
                            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-参数错误" });
                            return;
                        }
                        Ticket.RuleId = ruleDb.Id;
                    }

                    Ticket.Amount = Int32.Parse(Request["Amount"]);
                    Ticket.EndUseDate = DateTime.Parse(Request["EndUseDate"]);
                    Ticket.GotDate = DateTime.Parse(Request["GotDate"]);
                    Ticket.MemberAccount = Request["Member"];
                    Ticket.StartUseDate = DateTime.Parse(Request["StartUseDate"]);
                    Ticket.Status = Request["Status"];
                    Ticket.Title = Request["TicketTitle"];
                    Ticket.TicketCode = Request["TicketCode"];
                    Ticket.OrderMinAmount = int.Parse(Request["OrderMinAmount"]);
                    Ticket.CouponType = "coupon";

                    session.SaveOrUpdate(Ticket);
                    session.Flush();
                }


                //框架返回成功提示
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/Shop/Coupon/CouponTicketDetail.aspx?tabid=CouponTicketDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + Ticket.Id;
                }

                _framePage.Alert(rc);
            }
        }
    }
}